package com.ruoyi.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.utils.LocalDateTimeDeserializer;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@Data
@TableName("device_data")
public class DeviceData extends BaseEntity {
    @TableId(type = IdType.AUTO)
    private Integer id;
    @TableField("client_id")
    private String clientId;
    private String version;
    private Double temperature;
    private Double humidity;
    @TableField("sensor_id")
    private String sensorId;
    @TableField("collect_time")
    // LocalDateTime无法接收来之Arduino请求过来的时间戳格式例如:1750765388000
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime collectTime;

    /**
     * 数据库不存在该字段
     */
    @TableField(exist = false)
    private LocalDateTime collectTimes;

    /**
     * 数据库不存在该字段,为了解决返回给前端的时间少了8个小时
     */
    public LocalDateTime getCollectTimes() {
        return this.collectTime != null ? this.collectTime.plus(8, ChronoUnit.HOURS) : null;
    }

    /**
     * 重写基类BaseEntity中的createTime属性
     * 因为在新增数据的时候通过反射无法获取DeviceData类中的createTime属性
     * 创建时间
     */
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

}
